<?php
  $GLOBALS['THRIFT_ROOT'] = dirname(__FILE__).'/thrift';
  require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
  require_once $GLOBALS['THRIFT_ROOT'].'/transport/TTransport.php';
  require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
  require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
  require_once $GLOBALS['THRIFT_ROOT'].'/transport/TFramedTransport.php';
  require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
  require_once $GLOBALS['THRIFT_ROOT'].'/packages/fetcher_server/CrawlerServer.php';

  if (empty($_GET['url'])) {
    $path = '/';
  } else {
    $path = $_GET['url'];
  }
  $url = "http://www.education.com" . $path;
  $error_hint = "This page has not been downloaded, retry later or go to the " .
                "original page <a href='$url' target='_black' >$url</a>";

  try {
    $ip = "113.105.248.144";
    $port = 48600;
    $transport = new TSocket($ip, $port);
    $transport->setSendTimeout(30000);
    $transport->setRecvTimeout(30000);
    $protocol = new TBinaryProtocol($transport);

    $transport->open();
    $client= new CrawlerServerClient($protocol, $protocol);
    $page = $client->GetStoredPage($url);
    $transport->close();
    if ($page->status != PageStatus::kSuccessDownload) {
      echo $error_hint;
    } else {
      echo $page->content;
    }
  } catch (ServerError $err) {
    echo nl2br(print_r($err->why, true));
    echo $error_hint;
  } catch (Exception $err) {
    echo nl2br(print_r($err->getMessage(), true));
    echo $error_hint;
    return;
  }
?>
